package com.ihighest.app.infox.adapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.infox.R;

public class SearchResultListAdapter extends BaseAdapter {

	static final String TAG = "SearchResultListAdapter";
	Context context;
	JSONArray array;
	LayoutInflater inflate;

	public SearchResultListAdapter(Context context) {
		this.context = context;
		this.inflate = LayoutInflater.from(context);
	}

	public JSONArray set(JSONArray jsonArray) {
		this.array = jsonArray;
		return this.array;
	}

	public JSONArray append(JSONArray jsonArray) {
		try {
			this.array = AppKit.joinJSONArray(array, jsonArray);
		} catch (JSONException e) {
			Log.e(TAG, e.getMessage(), e);
		}
		return this.array;
	}

	@Override
	public int getCount() {
		return array != null ? array.length() : 0;
	}

	@Override
	public Object getItem(int arg0) {
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int potision, View contentview, ViewGroup arg2) {
		ViewHolder holder;
		if (contentview == null) {
			holder = new ViewHolder();
			contentview = inflate.inflate(R.layout.infox_activity_search_result_item, null);
			holder.tv_title = (TextView) contentview.findViewById(R.id.tv_title);
			holder.tv_source = (TextView) contentview.findViewById(R.id.tv_source);
			holder.tv_comment = (TextView) contentview.findViewById(R.id.tv_comment);
			holder.tv_time = (TextView) contentview.findViewById(R.id.tv_time);
			holder.iv_image = (ImageView) contentview.findViewById(R.id.iv_image);
			contentview.setTag(holder);
		} else {
			holder = (ViewHolder) contentview.getTag();
		}
		try {
			JSONObject json = array.getJSONObject(potision);
			holder.tv_title.setText(AppKit.trimString(json.getString("title")));
			if (json.has("source")) {
				holder.tv_source.setText(json.getString("source"));
			} else {
				holder.tv_source.setVisibility(View.GONE);
			}
			if (json.has("comments")) {
				holder.tv_comment.setText(json.getInt("comments") + " 条评论");
			} else {
				holder.tv_comment.setVisibility(View.GONE);
			}
			if (json.has("time")) {
				holder.tv_time.setText(AppKit.timeToBeforeStr(json.getLong("time")));
			} else {
				holder.tv_time.setVisibility(View.GONE);
			}
			if (json.has("image") && !TextUtils.isEmpty(json.getString("image"))) {
				holder.iv_image.setVisibility(View.VISIBLE);
				// Resources resources = context.getResources();
				// @SuppressWarnings("deprecation")
				// Drawable drawable =
				// resources.getDrawable(R.drawable.infox_img_loadrong);
				// DisplayImageOptions defaultOptions = new
				// DisplayImageOptions.Builder().showImageForEmptyUri(drawable)
				// .showImageOnFail(drawable).showImageOnLoading(drawable).cacheInMemory(true).cacheOnDisc(true).build();
				// ImageLoader.getInstance().displayImage(array.getJSONObject(potision).getString("image"),
				// holder.iv_image, defaultOptions);
				AppKit.SetNormalImg(array.getJSONObject(potision).getString("image"), holder.iv_image);
			} else {
				holder.iv_image.setVisibility(View.GONE);
			}
		} catch (JSONException e) {
			Log.e(TAG, e.getMessage(), e);
		}
		return contentview;
	}

	class ViewHolder {
		TextView tv_title, tv_source, tv_comment, tv_time;
		ImageView iv_image;
	}
}
